/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pgacluster;

import java.util.BitSet;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.baatar.opt.genetic.AlgorithmParameters;
import org.baatar.opt.pga.impl.client.PGACoordinatorConnection;
import org.baatar.opt.pga.impl.service.PGAClusterServiceImpl;
import org.baatar.remoting.service.RemotingService;
import org.baatar.util.XMLUtil;

/**
 *
 * @author Ba
 */
public class SCPGACluster {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        /**
         * Read cluster info from configuration file.
         */
        int portNr = Integer.valueOf(XMLUtil.readProperty("pgacluster.xml", "pgaCluster", "portNr"));
        Integer numOfWorkers = Integer.valueOf(XMLUtil.readProperty("pgacluster.xml", "pgaCluster", "numOfWorkers"));

        /**
         * Create implementation class and remoting service class.
         */
        PGAClusterServiceImpl<Boolean, BitSet> pgaClusterSrvImpl = new PGAClusterServiceImpl<Boolean, BitSet>();
        RemotingService pgaClusterService = new RemotingService(pgaClusterSrvImpl, portNr);

        if (pgaClusterService.initializeService()) {

            /**
             * Assign Logger.
             */
            Logger logger = Logger.getLogger(pgaClusterService.getRemotingServiceInfo().toString());
            pgaClusterService.setLogger(logger);
            logger.addAppender(new ConsoleAppender(new PatternLayout()));

            /**
             * Start cluster service.
             */
            if (pgaClusterService.startService()) {

                /**
                 * Read coordinator info from configuration file.
                 */
                String coordinatorHost = XMLUtil.readProperty("pgacluster.xml", "pgaCoordinator", "host");
                if (coordinatorHost != null) {
                    int coordinatorPort = Integer.valueOf(XMLUtil.readProperty("pgacluster.xml", "pgaCoordinator", "portNr"));

                    /**
                     * Create configurator client object.
                     */
                    PGACoordinatorConnection<Boolean, BitSet> pgaCoordinatorConnection = new PGACoordinatorConnection<Boolean, BitSet>();
                    pgaCoordinatorConnection.setHostName(coordinatorHost);
                    pgaCoordinatorConnection.setPortNo(coordinatorPort);
                    pgaCoordinatorConnection.setCallerInfo(pgaClusterService.getRemotingServiceInfo().toString());

                    /**
                     * Register cluster and get algortihm info from coordinator.
                     */
                    Integer clusterIndex = pgaCoordinatorConnection.registerCluster(pgaClusterService.getRemotingServiceInfo(), numOfWorkers);
                    if (clusterIndex != null) {
//                        pgaClusterSrvImpl.setCoordinator(pgaCoordinatorConnection);
                        pgaClusterSrvImpl.setNumOfWorkers(numOfWorkers);
                        pgaClusterSrvImpl.setClusterIndex(clusterIndex);

                        AlgorithmParameters<Boolean, BitSet> algoParams = pgaCoordinatorConnection.getAlgorithmParameters();
                        pgaClusterSrvImpl.setAlgoParams(algoParams);

                        pgaClusterService.getLogger().info("Ready.");
                    } else {
                        pgaClusterService.getLogger().info("Cluster is already registered.");
                    }
                } else {
                    pgaClusterSrvImpl.setNumOfWorkers(numOfWorkers);
                    pgaClusterService.getLogger().info("Ready without any coordinator registration.");                    
                }
            }
        }
    }
}
